package tech.aistar.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.entity.UserInfo;
import tech.aistar.entity.massage.Massage;
import tech.aistar.mapper.UserInfoMapper;
import tech.aistar.mapper.massage.MassageMapper;
import tech.aistar.service.IMassageService;
import tech.aistar.util.IdWorker;

import java.util.List;

@Service
@Transactional
public class MassageServiceImpl implements IMassageService {
    @Autowired
    private MassageMapper massageMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public List<Massage> findUidAndPost(Integer uId) {
        List<Massage> list = massageMapper.findUidAndPost(uId);
        return list;
    }

    @Override
    public List<Massage> findUidAndCom(Integer uId) {
        List<Massage> list = massageMapper.findUidAndCom(uId);
        return list;
    }

    @Override
    public List<Massage> findUidZpost(Integer uId) {
        List<Massage> list = massageMapper.findUidZpost(uId);
        return list;
    }

    @Override
    public List<Massage> findUidAndZCom(Integer uId) {
        List<Massage> list = massageMapper.findUidAndZCom(uId);
        return list;
    }

    @Override
    public List<Massage> findPicZan(Integer uId) {
        List<Massage> list = massageMapper.findPicZan(uId);
        return list;
    }

    @Override
    public void addPostMassage(Massage massage) {
        massage.setmId(idWorker.nextId());
        massageMapper.addPostMassage(massage);
        //找出这个人
        UserInfo userInfo = userInfoMapper.findById(massage.getuId());
        //评论数加一
        userInfoMapper.calComment(massage.getuId(),userInfo.getCommentNum()+1);
    }

    @Override
    public void addComMassage(Massage massage) {
        massage.setmId(idWorker.nextId());
        massageMapper.addComMassage(massage);
        //找出这个人
        UserInfo userInfo = userInfoMapper.findById(massage.getuId());
        //评论数加一
        userInfoMapper.calComment(massage.getuId(),userInfo.getCommentNum()+1);
    }

    @Override
    public void addPostZan(Massage massage) {
        massage.setmId(idWorker.nextId());
        massage.setContent(massage.getReName()+"对你的");
        massageMapper.addPostZan(massage);
        //找出这个人
        UserInfo userInfo = userInfoMapper.findById(massage.getuId());
        //点赞数加一
        userInfoMapper.calMyzan(massage.getuId(),userInfo.getMyzanNum()+1);
    }

    @Override
    public void addComZan(Massage massage) {
        massageMapper.addComZan(massage);
        //找出这个人
        UserInfo userInfo = userInfoMapper.findById(massage.getuId());
        //点赞数加一
        userInfoMapper.calMyzan(massage.getuId(),userInfo.getMyzanNum()+1);
    }

    @Override
    public void addPicZan(Massage massage) {
        massageMapper.addPicZan(massage);
        //找出这个人
        UserInfo userInfo = userInfoMapper.findById(massage.getuId());
        //点赞数加一
        userInfoMapper.calMyzan(massage.getuId(),userInfo.getMyzanNum()+1);
    }

    @Override
    public void delMassage(Long mId,Integer uId) {
        massageMapper.delMassage(mId,uId);
        //找出这个人
        UserInfo userInfo = userInfoMapper.findById(uId);
        //评论数加一
        userInfoMapper.calComment(uId,userInfo.getCommentNum()+1);
    }

    @Override
    public void ViewMassage(Long mId) {
        massageMapper.ViewMassage(mId);
    }

    @Override
    public List<Massage> findUnReadPost(Integer reId) {
        List<Massage> mass = massageMapper.findUnReadPost(reId);
        return mass;
    }

    @Override
    public List<Massage> findUnReadCom(Integer reId) {
        List<Massage> com = massageMapper.findUnReadCom(reId);
        return com;
    }
}
